﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoboServ
{
    class Utils
    {
        public static String byteArrayToString(byte[] buf)
        {
            StringBuilder sb = new StringBuilder();

            foreach (byte b in buf)
            {
                sb.Append((int)b + " ");
            }

            return sb.ToString();
        }

        public static void addArrayToArray(byte[] array, byte[] subArray, int startIndex = 0)
        {
            for (int i = 0; i < subArray.Length; i++)
                array[startIndex + i] = subArray[i];
        }

        public static byte[] getSubArray(byte[] array, int startIndex, int length)
        {
            byte[] ret = new byte[length];

            for (int i = 0; i < length; i++)
            {
                ret[i] = array[startIndex + i];
            }

            return ret;
        }

        public static UInt64 bytesToInt(byte[] b)
        {
            UInt64 value = 0;
            for (int i = 0; i < b.Length; i++)
                value = (value << 8) | b[i];
            return value;
        }

        public static byte[] UInt16ToBytes(UInt16 ui)
        {
            byte[] ret = new byte[2];

            for (int i = 8; i >= 0; i -= 8)
            {
                ret[(8 - i) / 8] = (byte)(ui >> i);
            }

            return ret;
        }

        public static byte[] UInt32ToBytes(UInt32 ui)
        {
            byte[] ret = new byte[4];

            for (int i = 24; i >= 0; i -= 8)
            {
                ret[(24 - i) / 8] = (byte)(ui >> i);
            }

            return ret;
        }

        public static byte[] UInt64ToBytes(UInt64 ui)
        {
            byte[] ret = new byte[8];

            for (int i = 56; i >= 0; i -= 8)
            {
                ret[(56 - i) / 8] = (byte)(ui >> i);
            }

            return ret;
        }
    }
}
